package com.cnc.fd.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.cnc.bas.util.Constants;
import com.cnc.bas.util.SpringBeanFactory;
import com.cnc.ssm.ISsmService;

/**
 * 任务类型和任务状态说明util
 * @author Administrator
 *
 */
public class TaskTipUtil {
    
    // 任务类型说明的代码类型
    private static final String TASK_KIND_TYPE = "0014";
    
    // 任务状态说明的代码类型
    private static final String TASK_STATUS_TYPE = "0015";
    
    // 任务类型说明map
    private static Map<String, String> taskKindTip = new HashMap<String, String>();
    
    // 任务状态说明map
    private static Map<String, String> taskStatusTip = new HashMap<String, String>();
    
    static{
        initTaskKindTip();
        initTaskStatusTip();
    }
    
    /**
     * 初始化任务类型说明
     */
    private static void initTaskKindTip(){
        ISsmService ssmService = (ISsmService) SpringBeanFactory.getBean(Constants.BEAN_NAME_SSM);
        List codeInfo = ssmService.listCodeInfo(TASK_KIND_TYPE);
        if(codeInfo == null || codeInfo.isEmpty()){
            defaultInitTaskKindTip();
            return;
        }
        Iterator iter = codeInfo.iterator();
        while (iter.hasNext()) {
            HashMap map = (HashMap) iter.next();
            taskKindTip.put(getRealValue(map.get("CODE").toString()), map.get("NAME").toString());
        }
    }
    
    
    /**
     * 初始化任务状态说明
     */
    private static void initTaskStatusTip(){
        ISsmService ssmService = (ISsmService) SpringBeanFactory.getBean(Constants.BEAN_NAME_SSM);
        List codeInfo = ssmService.listCodeInfo(TASK_STATUS_TYPE);
        if(codeInfo == null || codeInfo.isEmpty()){
            defaultInitTaskStatusTip();
            return;
        }
        
        Iterator iter = codeInfo.iterator();
        while (iter.hasNext()) {
            HashMap map = (HashMap) iter.next();
            taskStatusTip.put(getRealValue(map.get("CODE").toString()), map.get("NAME").toString());
        }
    }
    
    /**
     * 初始化任务类型说明（当数据库没配置时）
     */
    private static void defaultInitTaskKindTip(){
        taskKindTip.put("upload", "上传");
        taskKindTip.put("rename", "重命名");
        taskKindTip.put("mkdir", "创建文件夹");
        taskKindTip.put("delete", "删除");
        taskKindTip.put("decompress", "解压");
    }
    
    /**
     * 初始化任务状态说明（当数据库没配置时）
     */
    private static void defaultInitTaskStatusTip(){
        taskStatusTip.put("success", "成功");
        taskStatusTip.put("wait", "等待");
        taskStatusTip.put("failure", "失败");
        taskStatusTip.put("cancel", "取消");
        taskStatusTip.put("operator", "维护人员正在处理中");
        taskStatusTip.put("run", "文件正在同步");
        taskStatusTip.put("stop", "同步停止");
    }
    
    
    
    /**
     * 获取任务类型说明
     * @param taskKind
     * @return
     */
    public static String getTaskKindTip(String taskKind){
        if(taskKindTip.containsKey(taskKind)){
            return taskKindTip.get(taskKind);
        }
        return "";
    }
    
    /**
     * 获取任务状态说明
     * @param taskKind
     * @return
     */
    public static String getTaskStatusTip(String taskStatus){
        if(taskStatusTip.containsKey(taskStatus)){
            return taskStatusTip.get(taskStatus);
        }
        return "";
    }
    
    private static String getRealValue(String value){
        int pos = 0;
        if(value != null && (pos=value.lastIndexOf("-tip")) >= 0){
            return value.substring(0, pos);
        }
        return value;        
    }
    
    public static void main(String[] args){
        String abc = "upload-tip";
        System.out.println(getRealValue(abc));
    }

}
